package cn.web.tunnelrobot.websocket;

import cn.web.tunnelrobot.controller.WebSocketServerVoice;
import cn.web.tunnelrobot.util.Constants;
import com.alibaba.fastjson.JSONArray;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

@Slf4j
@Component  // 被Spring容器管理
@Order(3)   // 如果多个自定义ApplicationRunner，用来表明执行顺序
public class VoiceTalkback {
     static  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static void voiceIntercom() {
/*        WebSocketServerVoice.inetAddresses
        System.out.println("address:"+address);
        System.out.println("计算机名" + address.getHostName());
        //判断计算机名称是否一致(机器人的标识)
        if(address.getHostName().toUpperCase().equals("WIN-5BU5R40VPJD")) {*/
            Runnable runnable1 = new Runnable() {
                @SneakyThrows
                @Override
                public void run()  {
                    while (true) {
                        HashMap<String, Object> map = new HashMap<>();
                        Integer voiceControl = Constants.voiceControl;
                        if (voiceControl > 0) {
                            if (voiceControl == 1 || voiceControl == 2) {
                                //System.out.println("语音控制传入值:" + voiceControl);
                                if(Constants.connection == 1){
                                    map.put("code", 0);
                                    map.put("data", voiceControl);
                                    map.put("msg", "连接成功"+format.format(new Date()));
                                    //System.out.println("开启:" +Constants.connection);
                                    Constants.connection =0;
                                }else if(Constants.shutDown == 1){
                                    map.put("code", 0);
                                    map.put("data", voiceControl);
                                    map.put("msg", "连接成功"+format.format(new Date()));
                                    //System.out.println("结束:" +Constants.shutDown);
                                    Constants.shutDown =0;
                                }
                            } else {
                                map.put("code", -1);
                                map.put("msg", "请求超时"+format.format(new Date()));
                            }
                        } else {
                            map.put("code", -1);
                            map.put("msg", "请求错误"+format.format(new Date()));
                        }
                        try {
                            WebSocketServerVoice.sendInfo(JSONArray.toJSONString(map)); // 推送
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            Thread thread1 = new Thread(runnable1);
            thread1.start();
       // }
    }
}
